package com.chuangjiangx.member.basic.web.controller;

import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.wx.auth.WXWebAuthType;
import com.chuangjiangx.member.basic.ddd.application.EntryApplication;
import com.chuangjiangx.member.basic.ddd.application.LoginApplication;
import com.chuangjiangx.member.basic.ddd.domain.service.MemberRedisDomainService;
import com.chuangjiangx.member.coupon.web.response.response.EntryResponse;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author cj-ch
 * @date 2018/2/27 上午11:35
 */
@Slf4j
@Controller
@RequestMapping(value = "/h5/entry",produces = "application/json; charset=UTF-8")
@Api(value = "入口", tags = "入口", description = "会员中心入口")
public class EntryController extends BaseController{
    @Autowired
    private EntryApplication entryApplication;

    @Autowired
    private MemberRedisDomainService memberRedisDomainService;
    @Autowired
    private LoginApplication loginApplication;

    /**
     * 会员二期入口
     * @param response
     * @param merchantId
     */
    @RequestMapping
    public void membder2Entry(
            HttpServletResponse response,
            @RequestParam Long merchantId){

        // 如果不是ajax请求说明是二期老二维码
        try {
            response.sendRedirect(entryApplication.h5EntryUrl(merchantId));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @param request
     * @param merchantId
     * @return
     */
    @ApiOperation(value = "3期会员中心入口判断",httpMethod = "POST",notes = "尝试自动登录,如果不能则需要跳转登录页或重定向其他链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantId",value = "商户Id",paramType = "query")
    })
    @RequestMapping(value = "/mbr3")
    @ResponseBody
    public CamelResponse<EntryResponse> entry(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam Long merchantId){
        MbrUserContext userContext = loginService.currentUserContext();
        userContext.setMerchantId(merchantId);
        loginService.saveUserContext(userContext);
        if(StringUtils.isNotBlank(userContext.getMobile())){
            if(isValidLogin(userContext,merchantId)){
                //用户已经登录
//                loginAfter(modelAndView, userContext);
                return (CamelResponse<EntryResponse>)ResponseUtils.successCamel(new EntryResponse(1,null));
            }else{
                userContext = loginService.cleanUser(merchantId);
            }
        }
        // 用未登录或者不是会员
        String ua = request.getHeader("User-Agent");
        if(ua.contains("MicroMessenger")){
            //判断为微信
            if(StringUtils.isBlank(userContext.getOpenId())){
                // 获取服务商公众号openid尝试自动登录
                return (CamelResponse<EntryResponse>)ResponseUtils.successCamel(new EntryResponse(
                        3,
                        entryApplication.isvWxWebAuthUrl(merchantId,WXWebAuthType.BASE)
                ));
            }
            //去除判断用户上下文是否有商户公众号openid
//            else if (StringUtils.isBlank(userContext.getMopenId())){
//                return (CamelResponse<EntryResponse>)ResponseUtils.successCamel(new EntryResponse(
//                        3,
//                        entryApplication.merchantWxWebAuthUrl(merchantId,WXWebAuthType.BASE)
//                ));
//            }
            else{
                return (CamelResponse<EntryResponse>)ResponseUtils.successCamel(new EntryResponse(2,null));
            }

        }else if(ua.contains("AliApp")){
            //判断为支付宝

        }else if(ua.contains("Bestpay")){
            // 判断为翼支付

        }
        ////其他普通浏览器
        return (CamelResponse<EntryResponse>)ResponseUtils.successCamel(new EntryResponse(2,null));
    }



    @ApiOperation(value = "微信消息跳转至储值详情",httpMethod = "GET")
    @RequestMapping("wxmsg-store-detail")
    public ModelAndView wxmsgStoreDetail(@RequestParam Long storeId,
                                         @RequestParam Long merchantId){
        ModelAndView modelAndView = new ModelAndView("redirect:" + entryApplication.h5EntryUrl(merchantId));
        MbrUserContext userContext = loginService.currentUserContext();
        if(userContext != null && StringUtils.isNotBlank(userContext.getMobile())){
            if(isValidLogin(userContext,merchantId)){
                //用户已经登录
                modelAndView.setViewName("redirect:/app/index.html#/storeListDetail?storeId="+storeId);
                return modelAndView;
            }else{
                loginService.cleanUser(merchantId);
            }
        }
        return modelAndView;

    }


    @ApiOperation(value = "微信消息跳转至积分详情",httpMethod = "GET")
    @RequestMapping("wxmsg-score-detail")
    public ModelAndView wxmsgScoreSetail(@RequestParam Long pointId,
                                         @RequestParam Long merchantId){
        ModelAndView modelAndView = new ModelAndView("redirect:" + entryApplication.h5EntryUrl(merchantId));
        MbrUserContext userContext = loginService.currentUserContext();
        if(userContext != null && StringUtils.isNotBlank(userContext.getMobile())){

            if(isValidLogin(userContext,merchantId)){
                //用户已经登录
                modelAndView.setViewName("redirect:/app/index.html#/pointListDetail?id="+pointId);
                return modelAndView;
            }else{
                loginService.cleanUser(merchantId);
            }
        }
        return modelAndView;
    }
}
